from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from .serializers import *
from .models import *
from rest_framework.status import HTTP_404_NOT_FOUND,HTTP_201_CREATED,HTTP_204_NO_CONTENT

# Create your views here.

"""
    视图集：封装事物所有操作的类
    定义类继承ViewSet类
        定义对应的操作方法
            create 创建单条,list 查询所有,retrieve 查询单条,update 修改,delete 删除操作
"""


class StudentViewSet(viewsets.ModelViewSet):
    """
    list:
    查询所有学生信息
    """
    queryset = Student.objects.filter(is_delete=False)
    serializer_class = StudentSerializer



    # def list(self, request):
    #     """
    #     编写了查询所有学生的接口，根据restful设计风格定义路由，请求方式，请求地址
    #     :param request:
    #     :return:
    #     """
    #     students = Student.objects.filter(is_delete=False)  # 返回的是查询集
    #     serializer = StudentSerializer(students, many=True)  # 实例化序列化器，对查询集进行格式转换，将查询集类型转为json格式类型，many=True返回的数据是多条 []
    #     return Response(serializer.data)  # 序列化器对象.data是得到转换后的json格式数据
    # def retrieve(self,request,pk):
    #     #get
    #     try:
    #         student = Student.objects.filter(is_delete=False).get(id = pk)
    #     except Student.DoesNotExist:
    #         return Response(status=HTTP_404_NOT_FOUND)
    #     serializer = StudentSerializer(student)
    #
    #     return Response(serializer.data)
    #
    # def create(self,request):
    #     serializer = StudentSerializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #     return Response(serializer.data,status=HTTP_201_CREATED)
    #
    #
    # def update(self,request,pk):
    #     try:
    #         student = Student.objects.filter(is_delete=False).get(id=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=HTTP_404_NOT_FOUND)
    #
    #     serializer = StudentSerializer(data=request.data,instance=student)
    #     # serializer = StudentSerializer(student,request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #     return Response(serializer.data)
    #
    # def destory(self,request,pk):
    #     try:
    #         student = Student.objects.filter(is_delete=False).get(id=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=HTTP_404_NOT_FOUND)
    #
    #     student.delete()
    #     return Response(status=HTTP_204_NO_CONTENT)

class ClassViewset(viewsets.ModelViewSet):
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassSerializer

    @action(methods=['get'], detail=False)
    def last(self,request):
        classes = Classes.objects.last()
        # ClassSerializer_class(classes)
        return Response(self.get_serializer(classes).data)
#页面显示条数
class TopicPaginationPageNumber(PageNumberPagination):
    page_size = 20
    page_query_param = 'size'
    max_page_size = 100


# class MessageViewset(viewsets.ModelViewSet):
#     queryset = message.objects.all()
#     serializer_class = MessageSerializer
#
#     ###视图集增加分页器功能
#     pagination_class = TopicPaginationPageNumber







